﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wicresoft.EKA.ITSM.Business;
using Wicresoft.EKA.MVCExtensions;

namespace Wicresoft.EKA.ITSM.BusinessImpl
{
    public class SMGXJGroupAsso : Base, ISMGXJGroupAsso
    {
        protected override void Dispose(bool disposing)
        {
        }

        public List<SMGXJGroupAssoModel> GetList(string Qual)
        {
            var proxy = this.GetARProxy<SMGXJGroupAssoModel>();
            var list = proxy.GetListEntry(Qual, new System.Collections.Generic.List<uint>()
                {
                    SMGXJGroupAssoModel.GroupID_ID,
                    SMGXJGroupAssoModel.GroupName_ID,
                    SMGXJGroupAssoModel.Disabled_ID,
                    SMGXJGroupAssoModel.FullName_ID,
                    SMGXJGroupAssoModel.IsArtechGroup_ID,
                    SMGXJGroupAssoModel.LoginID_ID,
                    SMGXJGroupAssoModel.ShortDescription_ID
                }).ToList();
            return list;
        }

        public string Create(List<SMGXJGroupAssoModel> list)
        {
            try
            {
                var proxy = this.GetARProxy<SMGXJGroupAssoModel>();
                proxy.CreateListEntry(list);
                return "Success";
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public string Modify(string loginname)
        {
            try
            {
                SMGXJGroupAssoModel model = new SMGXJGroupAssoModel();
                model.Disabled = "Yes";
                var proxy = this.GetARProxy<SMGXJGroupAssoModel>();
                proxy.SetEntryByQuery(model, "'Disabled' = \"No\" AND 'LoginID' = \"" + loginname + "\"", BMC.ARSystem.Server.SetEntryByQuery_NoMatchOption.NoAction, BMC.ARSystem.Server.SetEntryByQuery_MultiMatchOption.ModifyAll);
                return "Success";
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public string GetGroupIDNameBy(string groupID)
        {
            var groupname = this.GetARProxy<GroupModel>().GetListEntry("'Group ID' = " + groupID , new System.Collections.Generic.List<uint>() 
                {
                    GroupModel.GroupName_ID
                }).Select(o=>o.GroupName).FirstOrDefault();

            return groupname.ToString();
        }

        public List<SMGXJGroupAssoModel> GetGroupName(string loginID)
        {
            var groupname = this.GetARProxy<SMGXJGroupAssoModel>().GetListEntry("'LoginID' = \"" + loginID + "\" AND 'Disabled' = \"No\" AND 'IsArtechGroup' = \"Yes\"", new System.Collections.Generic.List<uint>() 
                {
                    SMGXJGroupAssoModel.GroupName_ID,
                    SMGXJGroupAssoModel.GroupID_ID
                }).ToList();

            return groupname;
        }
    }
}
